package com.asistencia.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.asistencia.dao.interfaces.ProfessorCourseDao;
import com.asistencia.domain.Course;
import com.asistencia.domain.Professor;
import com.asistencia.domain.ProfessorCourse;
import com.asistencia.domain.Semester;

@Repository
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ProfessorCourseJdbc implements ProfessorCourseDao {

        @Autowired
        private JdbcTemplate jdbcTemplate;

        @Override
		public List<ProfessorCourse> listSemestersByProfessor(Integer idProfessor)
				throws DataAccessException {
			String SQL = "SELECT DISTINCT(idsemester) FROM professor_course WHERE idprofessor= ?";
			List matches = jdbcTemplate.query(SQL, new Object[] { idProfessor }, new RowMapper() {

                @Override
                public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

                        ProfessorCourse professorCourse = new ProfessorCourse();
                        Semester semester = new Semester();


                        semester.setIdSemester(rs.getInt("idsemester"));

                        professorCourse.setMySemester(semester);

                        return professorCourse;
                }
        });
        return (matches.size() > 0 ? (List<ProfessorCourse>) matches : null);
		}
        
        @Override
        public ProfessorCourse getById(Integer idProfesorCourse) throws DataAccessException {
                String SQL = "SELECT * FROM professor_course WHERE idprofesor_course = ?";
                List matches = jdbcTemplate.query(SQL, new Object[] { idProfesorCourse }, new RowMapper() {

                        @Override
                        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

                                ProfessorCourse professorCourse = new ProfessorCourse();
                                Professor professor = new Professor();
                                Semester semester = new Semester();
                                Course course = new Course();

                                professorCourse.setIdprofessorCourse(rs.getInt("idprofessor_course"));
                                professor.setIdProfessor(rs.getInt("idprofessor"));
                                semester.setIdSemester(rs.getInt("idsemester"));
                                course.setIdCourse(rs.getInt("idcourse"));

                                professorCourse.setMySemester(semester);
                                professorCourse.setMyCourse(course);
                                professorCourse.setMyprofessor(professor);
                                return professorCourse;
                        }
                });
                return (matches.size() > 0 ? (ProfessorCourse) matches.get(0) : null);
        }
        
        @Override
        public List<ProfessorCourse> listByProfessorSemester(Integer idProfessor, Integer idSemester)
                        throws DataAccessException {
                String SQL = "SELECT * FROM professor_course WHERE idprofessor = ? AND idsemester = ?";
                
                List matches = jdbcTemplate.query(SQL, new Object[] { idProfessor, idSemester }, new RowMapper() {

                        @Override
                        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

                                ProfessorCourse professorCourse = new ProfessorCourse();
                                Professor professor = new Professor();
                                Semester semester = new Semester();
                                Course course = new Course();

                                professorCourse.setIdprofessorCourse(rs.getInt("idprofessor_course"));
                                professor.setIdProfessor(rs.getInt("idprofessor"));
                                semester.setIdSemester(rs.getInt("idsemester"));
                                course.setIdCourse(rs.getInt("idcourse"));

                                professorCourse.setMySemester(semester);
                                professorCourse.setMyCourse(course);
                                professorCourse.setMyprofessor(professor);
                                return professorCourse;
                        }
                });
                return  matches.size() > 0 ? (List<ProfessorCourse>) matches : null;
        }

        @Override
        public List<ProfessorCourse> listAll() throws DataAccessException {
                String SQL = "SELECT * FROM ProfessorCourse";
                List matches = jdbcTemplate.query(SQL, new RowMapper() {

                        @Override
                        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

                                ProfessorCourse professorCourse = new ProfessorCourse();
                                Professor professor = new Professor();
                                Semester semester = new Semester();
                                Course course = new Course();

                                professorCourse.setIdprofessorCourse(rs.getInt("idprofessor_couse"));
                                professor.setIdProfessor(rs.getInt("idprofessor"));
                                semester.setIdSemester(rs.getInt("idsemester"));
                                course.setIdCourse(rs.getInt("idcourse"));

                                professorCourse.setMySemester(semester);
                                professorCourse.setMyCourse(course);
                                professorCourse.setMyprofessor(professor);
                                return professorCourse;
                        }
                });

                return (matches.size() > 0 ? (List<ProfessorCourse>) matches : null);
        }

        @Override
        public List<ProfessorCourse> listByIdSemester(Integer idSemester) throws DataAccessException {
                String SQL = "SELECT * FROM ProfessorCourse WHERE idsemester =?";
                List matches = jdbcTemplate.query(SQL, new Object[] { idSemester }, new RowMapper() {

                        @Override
                        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                                ProfessorCourse professorCourse = new ProfessorCourse();
                                Professor professor = new Professor();
                                Semester semester = new Semester();
                                Course course = new Course();

                                professorCourse.setIdprofessorCourse(rs.getInt("idprofessor_course"));
                                professor.setIdProfessor(rs.getInt("idprofessor"));
                                semester.setIdSemester(rs.getInt("idsemester"));
                                course.setIdCourse(rs.getInt("idcourse"));

                                professorCourse.setMySemester(semester);
                                professorCourse.setMyCourse(course);
                                professorCourse.setMyprofessor(professor);
                                return professorCourse;
                        }
                });

                return (matches.size() > 0 ? (List<ProfessorCourse>) matches : null);
        }

        @Override
        public List<ProfessorCourse> listByIdCourse(Integer idCourse) throws DataAccessException {
                String SQL = "SELECT * FROM ProfessorCourse WHERE idcourse =?";
                List matches = jdbcTemplate.query(SQL, new Object[] { idCourse }, new RowMapper() {

                        @Override
                        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                                ProfessorCourse professorCourse = new ProfessorCourse();
                                Professor professor = new Professor();
                                Semester semester = new Semester();
                                Course course = new Course();

                                professorCourse.setIdprofessorCourse(rs.getInt("idprofessor_course"));
                                professor.setIdProfessor(rs.getInt("idprofessor"));
                                semester.setIdSemester(rs.getInt("idsemester"));
                                course.setIdCourse(rs.getInt("idcourse"));

                                professorCourse.setMySemester(semester);
                                professorCourse.setMyCourse(course);
                                professorCourse.setMyprofessor(professor);
                                return professorCourse;
                        }
                });

                return (matches.size() > 0 ? (List<ProfessorCourse>) matches : null);
        }

        @Override
        public List<ProfessorCourse> listByIdProfessor(Integer idProfesorCourse)
                        throws DataAccessException {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public List<ProfessorCourse> listByProfessorCourse(Integer idProfessor, Integer idCourse)
                        throws DataAccessException {
                // TODO Auto-generated method stub
                return null;
        }

		@Override
		public List<ProfessorCourse> listaByCourse(Integer idcourse)
				throws DataAccessException {
			// TODO Auto-generated method stub
			return null;
		}

}
